package bg.cinema_system.beans;

import java.sql.SQLException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.NamingException;

import bg.cinema_system.pojo.DailyProgram;
import bg.cinema_system.service.DailyProgramService;

@ManagedBean(name = "dailyProgramList")
@SessionScoped
public class DailyProgramListBean {

	private Map<String, List<DailyProgram>> dates;

	public DailyProgramListBean() throws NamingException, SQLException {
		dates = new LinkedHashMap<String, List<DailyProgram>>();
		List<DailyProgram> dailyProgram = new ArrayList<DailyProgram>();

		String dateToMap;
		Date date;
		Format formatter;
		Calendar calendar = Calendar.getInstance();

		dailyProgram = getMovieList(0);
		date = calendar.getTime();
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		dateToMap = formatter.format(date);
		dates.put(dateToMap.toString(), dailyProgram);

		dailyProgram = getMovieList(1);
		calendar.add(Calendar.DATE, 1);
		date = calendar.getTime();
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		dateToMap = formatter.format(date);
		dates.put(dateToMap.toString(), dailyProgram);

		dailyProgram = getMovieList(2);
		calendar.add(Calendar.DATE, 1);
		date = calendar.getTime();
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		dateToMap = formatter.format(date);
		dates.put(dateToMap.toString(), dailyProgram);

		dailyProgram = getMovieList(3);
		calendar.add(Calendar.DATE, 1);
		date = calendar.getTime();
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		dateToMap = formatter.format(date);
		dates.put(dateToMap.toString(), dailyProgram);

		dailyProgram = getMovieList(4);
		calendar.add(Calendar.DATE, 1);
		date = calendar.getTime();
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		dateToMap = formatter.format(date);
		dates.put(dateToMap.toString(), dailyProgram);
	}

	public Map<String, List<DailyProgram>> getDates() {
		return dates;
	}

	public Set<String> getKeys() {
		return dates.keySet();
	}

	// Connect to DB and get customer list
	public List<DailyProgram> getMovieList(int interval) throws SQLException {
		List<DailyProgram> dailyProgramList = new ArrayList<DailyProgram>();
		dailyProgramList = DailyProgramService.getDailyProgramList(interval);

		return dailyProgramList;
	}
}